<?php

namespace App\Models\Index;

use App\Models\User\LiveUserCollect;
use Illuminate\Database\Eloquent\Model;

class LiveMovie extends Model
{
    public $table = 'live_movie';
    public $timestamps = false;

    public function getCreatedTimeAttribute($val)
    {
        return date('Y-m-d',$val);
    }

    #获取热门电影列表
    public static function getHotMovieList(int $attribute)
    {
        $cate_ids = LiveMovieCate::getCateIds($attribute);
        $data = self::query()->whereIn('cate_id',$cate_ids)->orderByDesc('click_count')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #获取指定的电影
    public static function getAppointMovie(int $id, int $user_id)
    {
        $data = self::with(['collect' => function ($query) use ($user_id) {
                    $query->where('user_id','=',$user_id);
                }])->find($id);
        if (empty($data)) {
            return [];
        } else {
            self::query()->where('id','=',$id)->increment('click_count',1);
            $data->click_count += 1;
            return $data;
        }
    }

    #获取推荐电影列表
    public static function getRecommendMovieList(int $cate_id)
    {
        $data = self::query()->where('cate_id','=',$cate_id)->orderByDesc('click_count')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #根据分类id获取电影列表
    public static function getMovieByCateId(int $cate_id)
    {
        $data = self::query()->where('cate_id','=',$cate_id)->orderByDesc('click_count')->paginate(15)->toArray();
        if (empty($data)) {
            return [];
        } else {
            return $data;
        }
    }

    #根据电影id自增收藏量
    public static function incCollectById(int $id)
    {
        $flag = self::query()->where('id','=',$id)->increment('collect_count',1);
        return $flag;
    }

    public function collect()
    {
        return $this->hasOne(LiveUserCollect::class,'movie_id','id')->select('movie_id','status')->withDefault([
            'status' => '0',
        ]);
    }


    #电影列表分页
    public static function getMovieListPaginate()
    {
        $data = self::query()
                ->join('live_movie_cate','live_movie_cate.cate_id','=','live_movie.cate_id')
                ->select('live_movie_cate.cate_name','live_movie_cate.attribute','live_movie.*')
                ->orderByDesc('live_movie.click_count')
                ->paginate(25)
                ->toArray();
        return $data;
    }
}
